package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.model.record.RecordTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2018/4/20.
 */
public final class ApiRecordNotifyUtil {

    private static Logger logger = LoggerFactory.getLogger(ApiRecordNotifyUtil.class);
    private static final String PORT = Configuration.getString("record.task.port");

    private ApiRecordNotifyUtil() {
    }

    public static JSONObject notifyStart(long id, String ip, String address, String channelCode) {
        String url = "http://" + ip + ":" + PORT + "/start";
        Map<String, String> params = new HashMap<String, String>();
        params.put("signal_id", id + "");
        params.put("channel_code", channelCode);
        try {
            params.put("url", Base64.encode(address.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException ex) {
            logger.error(" trans address exception " + address, ex);
        }
        JSONObject retObj = new JSONObject();
        String retValue = ApiHttpRequestUtil.doGet(url, null, params);
        if (retValue != null && retValue.length() > 2) {
            retObj = JSONObject.parseObject(retValue);
        }
        return retObj;
    }


    public static JSONObject notifyStop(long id,String ip) {
        String url = "http://" + ip + ":" + PORT + "/stop";
        Map<String, String> params = new HashMap<String, String>();
        params.put("signal_id", id + "");
        JSONObject retObj = new JSONObject();
        String retValue = ApiHttpRequestUtil.doGet(url, null, params);
        if (retValue != null && retValue.length() > 2) {
            retObj = JSONObject.parseObject(retValue);
        }
        return retObj;
    }

    public static JSONObject notifyGetVideo(RecordTask task) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", Constants.CODE_ERROR_PARAM);
        try {
            logger.info("[ApiRecordNotifyUtil] notifyGetVideo --> " + task);
            HashMap<String, String> params = task.getParams();
            String signal_id = params.get("signal_id");
            String channel_code = params.get("channel_code");
            String taskname = Base64.encode(params.get("taskname").getBytes("UTF-8"));
            String channelName = Base64.encode(params.get("channel_name").getBytes("UTF-8"));
            String copyright = params.get("copyright");
            String classfy = params.get("classfy");
            String isLogo = params.get("is_logo").equals("true") ? "1" : "0";
            String material_name = Base64.encode(params.get("material_name").getBytes("UTF-8"));
            String serverIp = params.get("serverIp");

            String url = "http://" + serverIp + ":" + PORT+"/get";
            Map<String,String> sendParams = new HashMap<String,String>();
            sendParams.put("signal_id",signal_id);
            sendParams.put("channel_code",channel_code);
            sendParams.put("begin",Timestamp.valueOf(task.getTaskStartTime()).getTime()/1000+"");
            sendParams.put("end",Timestamp.valueOf(task.getTaskStopTime()).getTime()/1000+"");
            sendParams.put("taskid",task.getId()+"");
            sendParams.put("taskname",taskname);
            sendParams.put("copyright",copyright);
            sendParams.put("classfy",classfy);
            sendParams.put("channel_name",channelName);
            sendParams.put("material_name",material_name);
            sendParams.put("is_logo",isLogo);
            jsonObject = JSON.parseObject(ApiHttpRequestUtil.doGet(url, null, sendParams));
        } catch (Exception e) {
            jsonObject.put("msg", e);
            logger.info("notifyGetVideo[exception=" + e + "][result=" + jsonObject.toJSONString());
        }
        return jsonObject;
    }

}
